package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.HelpCenterDto;
import com.wxzz.elearing.user.dto.PlatformParamDto;
import com.wxzz.elearing.user.dto.PlatformParamSelDto;
import com.wxzz.elearing.user.entity.HelpCenter;
import com.wxzz.elearing.user.entity.PlatformParam;
import com.wxzz.elearing.user.mapper.HelpCenterMapper;
import com.wxzz.elearing.user.mapper.PlatformParamMapper;
import com.wxzz.elearing.user.service.IPlatformParamService;
import com.wxzz.elearing.user.vo.PlatformParamVo;
import com.wxzz.elearning.common.constants.RedisConstants;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class PlatformParamImpl extends ServiceImpl<PlatformParamMapper, PlatformParam> implements IPlatformParamService {

    @Autowired
    private PlatformParamMapper platformParamMapper;


    @Override
    public ReturnVO listPlatformParam(PlatformParamDto platformParamDto) {
        if (platformParamDto == null) {
            throw new ServiceException("请传入showType");
        }
        if (platformParamDto.getShowType() == 0) {
            PageHelper.startPage(platformParamDto.getCurPage(), platformParamDto.getMaxPage());
            QueryWrapper<PlatformParam> queryWrapper = new QueryWrapper();
            if (platformParamDto.getShowType() != null) {
                queryWrapper.eq("show_type", platformParamDto.getShowType());
            }
            List<PlatformParam> platformParams = platformParamMapper.selectList(queryWrapper);
            return new ReturnVO<>("平台参数查询成功", new JgPageVo<>(new PageInfo(platformParams)));
        }
        if (platformParamDto.getShowType() == 1) {
            List<PlatformParam> platformParams = platformParamMapper.selectList(new LambdaQueryWrapper<PlatformParam>()
                    .eq(PlatformParam::getShowType, 1));
            Map<String, String> map = new HashMap<>();
            for (PlatformParam platformParam : platformParams) {
                map.put(platformParam.getParamKey(), platformParam.getParamDetail());
            }
            return new ReturnVO<>("平台参数查询成功", map);
        }
        if (platformParamDto.getShowType() == 2) {
            List<PlatformParam> platformParams = platformParamMapper.selectList(new LambdaQueryWrapper<PlatformParam>()
                    .eq(PlatformParam::getShowType, 2));
            Map<String, String> map = new HashMap<>();
            for (PlatformParam platformParam : platformParams) {
                map.put(platformParam.getParamKey(), platformParam.getParamDetail());
            }
            return new ReturnVO<>("平台参数查询成功", map);
        }
        if (platformParamDto.getShowType() == 3) {
            List<PlatformParam> platformParams = platformParamMapper.selectList(new LambdaQueryWrapper<PlatformParam>()
                    .and(qw -> qw.eq(PlatformParam::getShowType, 3).or().eq(PlatformParam::getShowType, 4).or().eq(PlatformParam::getShowType, 5).or().eq(PlatformParam::getShowType, 6)));
            Map<String, String> map = new HashMap<>();
            for (PlatformParam platformParam : platformParams) {
                map.put(platformParam.getParamKey(), platformParam.getParamDetail());
            }
            return new ReturnVO<>("平台参数查询成功", map);
        }
        return null;

    }

    @Override
    public ReturnVO updatePlatformParam(PlatformParam platformParam) {
        platformParam.setShowType(null);
        platformParamMapper.updateById(platformParam);
        return new ReturnVO<>("平台参数查询成功", platformParam.getId());
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public ReturnVO updatePlatformParamUnList(Map<String, String> map) {
        map.forEach((k, v) -> {
            PlatformParam platformParam = platformParamMapper.selectOne(new LambdaQueryWrapper<PlatformParam>()
                    .eq(PlatformParam::getParamKey, k));
            Optional.ofNullable(platformParam).ifPresent(p -> {
                //存入缓存
                redisTemplate.opsForValue().set(RedisConstants.SMS_PARAM + k, v);
                p.setParamDetail(v);
                platformParamMapper.updateById(p);
            });
        });
        return new ReturnVO<>("平台参数修改成功", null);
    }

    @Override
    public ReturnVO<PlatformParam> selectOnePlatformParam(PlatformParamSelDto platformParamSelDto) {
        PlatformParam platformParam = platformParamMapper.selectOne(new LambdaQueryWrapper<PlatformParam>().eq(PlatformParam::getParamName, platformParamSelDto.getParamName()));
        return new ReturnVO<>("平台参数查询成功", platformParam);
    }

    @Autowired
    private HelpCenterMapper helpCenterMapper;

    @Override
    public ReturnVO saveHelpCenter(HelpCenter helpCenter) {
        if (helpCenter.getId() == null) {
            helpCenterMapper.insert(helpCenter);
        } else {
            helpCenterMapper.updateById(helpCenter);
        }
        return new ReturnVO<>("帮助中心新增或修改成功", helpCenter.getId());
    }

    @Override
    public ReturnVO<Map<String, String>> selectPlatformParamVo() {
        List<PlatformParam> platformParams = platformParamMapper.selectList(new LambdaQueryWrapper<PlatformParam>()
                .eq(PlatformParam::getShowType, 5));
        Map<String, String> map = new HashMap<>();
        map.put("type", "");
        for (PlatformParam platformParam : platformParams) {
            map.put(platformParam.getParamKey(), platformParam.getParamDetail());
        }
        return new ReturnVO("查询所有小程序审核功能设置", map);
    }

    @Override
    public ReturnVO<Map<String, String>> selectPlatformParamPhoneVo() {
        List<PlatformParam> platformParams = platformParamMapper.selectList(new LambdaQueryWrapper<PlatformParam>()
                .eq(PlatformParam::getShowType, 3));
        Map<String, String> map = new HashMap<>();
        for (PlatformParam platformParam : platformParams) {
            map.put(platformParam.getParamKey(), platformParam.getParamDetail());
        }
        return new ReturnVO("客服电话查询成功", map);
    }

    @Override
    public ReturnVO deleteHelpCenter(Long helpCenterId) {
        helpCenterMapper.deleteById(helpCenterId);
        return new ReturnVO("帮助中心删除成功", helpCenterId);
    }

    @Override
    public ReturnVO listHelpCenter(HelpCenterDto helpCenterDto) {
        PageHelper.startPage(helpCenterDto.getCurPage(), helpCenterDto.getMaxPage());
        QueryWrapper<HelpCenter> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(helpCenterDto.getHelpTitle())) {
            queryWrapper.like("help_title", helpCenterDto.getHelpTitle() + "%");
        }
        List<HelpCenter> helpCenters = helpCenterMapper.selectList(queryWrapper);
        return new ReturnVO("帮助中心删除成功", new JgPageVo<>(new PageInfo(helpCenters)));
    }

    @Override
    public ReturnVO<HelpCenter> selectHelpCenterDetail(Long helpCenterId) {
        HelpCenter helpCenter = helpCenterMapper.selectById(helpCenterId);
        return new ReturnVO("查询帮助中心详情", helpCenter);
    }

    @Override
    public String selectOne(String key) {
        PlatformParam platformParam = platformParamMapper.selectOne(new LambdaQueryWrapper<PlatformParam>()
                .eq(PlatformParam::getParamKey, key));
        if (platformParam == null) {
            return "";
        }
        return platformParam.getParamDetail();
    }
}
